"""
Name:abnerclc
Email:abnerclc@hotmail.com
Date:2024.10.12
Env:Python3.6
并发模型类完成
"""
import sys
from threading import Thread
from socket import *

HOST = "0.0.0.0"
PORT = 8888
ADDR = (HOST, PORT)

class Handle:
    def request(self,data):
        print(data)



class ThreadServer(Thread):
    def __init__(self, conn):
        self.conn = conn
        self.handle = Handle()
        super().__init__(daemon=True)
    def run(self):
        while True:
            data = self.conn.recv(1024).decode()
            if not data:
                break
            self.handle.request(data)
            self.conn.close()
class ConnectClient:
    def __init__(self):
        self.sock = socket()
        self.sock.bind(ADDR)
        self.sock.listen(5)

    def main(self):

        print("listen the port:%d" % PORT)
        while True:
            try:
                conn, addr = self.sock.accept()
                print("connect from", addr)
            except KeyboardInterrupt:
                self.sock.close()
                sys.exit("服务结束")
            t = ThreadServer(conn)
            t.start()





if __name__ == '__main__':
    cc = ConnectClient()
    cc.main()
